<?php

namespace common\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\SysUser;

/**
 * SearchUser represents the model behind the search form about `common\models\SysUser`.
 */
class SearchUser extends SysUser
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'qq', 'sex', 'status'], 'integer'],
            [['login_account', 'user_name', 'password', 'email', 'last_ip', 'last_login_time', 'reg_time', 'icon'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = SysUser::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'qq' => $this->qq,
            'last_login_time' => $this->last_login_time,
            'reg_time' => $this->reg_time,
            'sex' => $this->sex,
            'status' => $this->status,
        ]);

        $query->andFilterWhere(['like', 'login_account', $this->login_account])
            ->andFilterWhere(['like', 'user_name', $this->user_name])
            ->andFilterWhere(['like', 'password', $this->password])
            ->andFilterWhere(['like', 'email', $this->email])
            ->andFilterWhere(['like', 'last_ip', $this->last_ip])
            ->andFilterWhere(['like', 'icon', $this->icon]);

        return $dataProvider;
    }
}
